package com.hongliang.netty02.protocol;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * redis协议：
 * set name zhangsan :
 * *3 : 表示这个命令有三个
 * $3 ： set的长度为3
 * set
 * $4 ： name长度为4
 * name
 * $8：zhangsan的长度为8
 * zhangsan
 *
 * @author Hongliang Zhu
 * @create 2022-11-08 22:33
 */
public class TestRedis {
    /**
     * 回车换行符
     */
    static byte[] LINE = {13, 10};

    public static void main(String[] args) throws InterruptedException {
        new Bootstrap().channel(NioSocketChannel.class)
                .group(new NioEventLoopGroup())
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                // 建立连接的时候发送命令
                                ByteBuf buffer = ctx.alloc().buffer();

                                buffer.writeBytes("*3".getBytes());
                                buffer.writeBytes(LINE);
                                buffer.writeBytes("$3".getBytes());
                                buffer.writeBytes(LINE);

                                buffer.writeBytes("set".getBytes());
                                buffer.writeBytes(LINE);

                                buffer.writeBytes("$4".getBytes());
                                buffer.writeBytes(LINE);

                                buffer.writeBytes("name".getBytes());
                                buffer.writeBytes(LINE);

                                buffer.writeBytes("$8".getBytes());
                                buffer.writeBytes(LINE);

                                buffer.writeBytes("zhangsan".getBytes());
                                buffer.writeBytes(LINE);


                                ctx.writeAndFlush(buffer);

                                get(ctx);

                            }
                            private void get(ChannelHandlerContext ctx) {
                                ByteBuf buf = ctx.alloc().buffer();
                                buf.writeBytes("*2".getBytes());
                                buf.writeBytes(LINE);
                                buf.writeBytes("$3".getBytes());
                                buf.writeBytes(LINE);
                                buf.writeBytes("get".getBytes());
                                buf.writeBytes(LINE);
                                buf.writeBytes("$4".getBytes());
                                buf.writeBytes(LINE);
                                buf.writeBytes("name".getBytes());
                                buf.writeBytes(LINE);
                                ctx.writeAndFlush(buf);
                            }

                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                // 获取redis服务端的消息
                                ByteBuf buffer = (ByteBuf) msg;
                                System.out.println(buffer.toString(Charset.defaultCharset()));
                            }
                        });

                    }
                })
                .connect(new InetSocketAddress("localhost", 6379))
                .sync();


    }
}
